package com.jianyun.jyzs.widget;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 面包屑组件
 * 可对该组件做添加、删除节点操作
 */
public class CrumbView extends LinearLayout {

    private LinearLayout containerLinearLayout;
    private OnPieceClickListener onPieceClickListener;
    private float textSize;
    private int padding;
    private List<Object> tagList = new ArrayList<>();

    public CrumbView(Context context) {
        super(context);
        init(null);
    }

    public CrumbView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        inflate(getContext(), R.layout.rce_view_crumb, this);
        containerLinearLayout = (LinearLayout) findViewById(R.id.ll_crumb_container);
        textSize = getResources().getDimension(R.dimen.rce_text_size_16);
        padding = (int) getResources().getDimension(R.dimen.rce_dimen_size_5);
    }

    public void setRootPiece(String text, Object tag) {
        TextView tv = new TextView(getContext());
        tv.setText(text);
        tv.setTextColor(getResources().getColor(R.color.color_gray_text));
        tv.setTag(tag);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onPieceClickListener.onPieceClick(v.getTag());
            }
        });
        tv.setClickable(false);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        containerLinearLayout.removeAllViews();
        containerLinearLayout.addView(tv);
        tagList.clear();
        tagList.add(tag);
    }

    /**
     * 在 root 节点后增加子节点
     *
     * @param text 节点内容
     * @param tag  节点唯一标识，当节点被点击时，回调方法中携带 tag 标识对应的节点
     */
    public void addPiece(@NonNull String text, @NonNull Object tag) {
        int childCount = containerLinearLayout.getChildCount();
        if (childCount > 0) {
            View child = containerLinearLayout.getChildAt(childCount - 1);
            if (child instanceof TextView) {
                child.setClickable(true);
                ((TextView) child).setTextColor(getResources().getColor(R.color.color_primary));
            }
        }
        TextView tv = new TextView(getContext());
        tv.setText(text);
        tv.setTextColor(getResources().getColor(R.color.color_gray_text));
        tv.setTag(tag);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onPieceClickListener.onPieceClick(v.getTag());
            }
        });

        ImageView iv = new ImageView(getContext());
        iv.setImageResource(R.drawable.rce_ic_right_arrow);
        iv.setPadding(padding, 0, padding, 0);
        containerLinearLayout.addView(iv);
        containerLinearLayout.addView(tv);
        tagList.add(tag);
    }

    /**
     * 回退到某一个节点，回退后，此节点后的节点会被清除。
     *
     * @param tag 节点唯一标识
     */
    public void backPieceTo(@NonNull Object tag) {
        int childCount = containerLinearLayout.getChildCount();
        int tagIndex = -1;
        for (int i = childCount - 1; i >= 0; i--) {
            View child = containerLinearLayout.getChildAt(i);
            if (tag.equals(child.getTag())) {
                tagIndex = i;
                break;
            }
        }
        if (tagIndex == -1) {
            throw new IllegalArgumentException("Not include tag : " + tag);
        } else {
            for (int i = childCount - 1; i > tagIndex; i--) {
                containerLinearLayout.removeViewAt(i);
            }
            View child = containerLinearLayout.getChildAt(tagIndex);
            if (child instanceof TextView) {
                child.setClickable(false);
                ((TextView) child).setTextColor(getResources().getColor(R.color.color_gray_text));
            }
        }
        int size = tagList.size();
        for (int i = size - 1; i >= 0; i--) {
            if (tagList.get(i).equals(tag)) {
                break;
            } else {
                tagList.remove(i);
            }
        }
    }

    /**
     * 清除除 root 节点的的所有节点
     */
    public void clearPiece() {
        int childCount = containerLinearLayout.getChildCount();
        for (int i = childCount - 1; i > 0; i--) {
            containerLinearLayout.removeViewAt(i);
        }
        childCount = containerLinearLayout.getChildCount();
        if (childCount == 1) {
            View child = containerLinearLayout.getChildAt(0);
            ((TextView) child).setTextColor(getResources().getColor(R.color.color_gray_text));
        }
        tagList.clear();
    }

    /**
     *
     * @return tag
     */
    public Object popPiece() {
        int childCount = containerLinearLayout.getChildCount();
        if (childCount > 2) {
            containerLinearLayout.removeViewAt(childCount - 1);
            containerLinearLayout.removeViewAt(childCount - 2);
            View child = containerLinearLayout.getChildAt(childCount - 3);
            if (child instanceof TextView) {
                child.setClickable(false);
                ((TextView) child).setTextColor(getResources().getColor(R.color.color_gray_text));
            }
        }
        return tagList.remove(tagList.size() - 1);
    }

    public int getPieceCount() {
        return tagList.size();
    }

    public int getPiecePosition(@NonNull Object tag) {
        return tagList.indexOf(tag);
    }

    /**
     * 设置节点监听，当节点被点击时，回调对应节点的点击事件及节点的唯一标识符
     */
    public void setOnPieceClickListener(OnPieceClickListener listener) {
        onPieceClickListener = listener;
    }

    public interface OnPieceClickListener {
        /**
         * 节点被点击时的回调
         *
         * @param tag 节点唯一标识
         */
        void onPieceClick(@NonNull Object tag);
    }

    public static class CrumbNode implements Parcelable {

        public String name;
        public String id;


        public CrumbNode(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public static final Creator<CrumbNode> CREATOR = new Creator<CrumbNode>() {
            public CrumbNode createFromParcel(Parcel source) {
                CrumbNode node = new CrumbNode(source.readString(), source.readString());
                return node;
            }

            public CrumbNode[] newArray(int size) {
                return new CrumbNode[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeString(id);
        }
    }
}
